module.exports = {
	/**
	 * 查询多条记录 分页
	 * @url admin/recharge/pub/getList 前端调用的url参数地址
	 * data 请求参数 说明
	 * @params {Number}         pageIndex 当前页码
	 * @params {Number}         pageSize  每页显示数量
	 * @params {Array<Object>}  sortRule  排序规则
	 * @params {object}         formData  查询条件数据源
	 * @params {Array<Object>}  columns   查询条件规则
	 * res 返回参数说明
	 * @params {Number}         code      错误码，0表示成功
	 * @params {String}         msg       详细信息
	 */
	main: async (event) => {
		const db = uniCloud.database();

		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			formData,
			_add_time,
			out_trade_no,
			pay_status,
			pageIndex,
			pageSize
		} = data;
		let res = {
			code: 0,
			msg: ''
		};
		// 业务逻辑开始-----------------------------------------------------------
		let dbName = "a_recharge"; // 表名
		data = {
			pageIndex: pageIndex,
			pageSize: pageSize,
			formData: {
				tv_name: formData.tv_name,
				_add_time: formData._add_time,
			},
			columns: [{
					key: "tv_name",
					title: "昵称",
					type: "text",
					width: 160,
					mode: "="
				},
				{
					key: "_add_time",
					title: "添加时间",
					type: "datetimerange",
					width: 400,
					mode: "[]"
				}
			],
			sortRule: []
		};

		if (!formData.querykey || formData.querykey == 'key') {
			res.code = 1;
			res.msg = "查询密钥不能为空或key";
			return res;
		}

		if (!formData.tv_name || formData.tv_name == '直充') {
			res.code = 1;
			res.msg = "剧名不能为空或直充";
			return res;
		}

		//	检查短剧绑定的秘钥是否为空或是否和用户输入的查询密钥一致，为空或不一致则不允许查询。
		let tvInfo = await db.collection('a_tv').where({
			tv_name: formData.tv_name
		}).get()
		if (tvInfo.data.length == 0) {
			res.code = 1;
			res.msg = "系统中不存在该剧，请核对剧名";
			return res;
		}

		if (!tvInfo.data[0].querykey || tvInfo.data[0].querykey != formData.querykey) {
			res.code = 1;
			res.msg = "查询密钥错误";
			return res;
		}

		if (userInfo.role.includes("copyright") || userInfo.role.includes("admin")) {
			console.log("权限校验通过");
		} else {
			res.code = 1;
			res.msg = "用户身份校验不通过";
			return res;
		}

		console.log("进入查询=", formData.tv_name);
		console.log("查询时间=", formData._add_time);
		res = await vk.baseDao.getTableData({
			dbName,
			data,
			whereJson: {
				pay_status: 1,
				// tv_name: formData.tv_name || "剧名"
			},
			sortArr: [{
				"name": "_add_time",
				"type": "desc"
			}]
		});

		return res;

	}

}